#!/bin/bash
# 启动服务脚本

echo "🚀 启动质量看板系统服务..."

# 检查PostgreSQL服务
if ! systemctl is-active --quiet postgresql; then
    echo "⚠️  PostgreSQL服务未运行，正在启动..."
    sudo systemctl start postgresql
    sleep 2
fi

# 启动后端服务
echo "📦 启动后端服务..."
cd backend
source venv/bin/activate
uvicorn app.main:app --reload --port 8000 &
BACKEND_PID=$!
echo "✅ 后端服务已启动 (PID: $BACKEND_PID)"
echo "   - API文档: http://localhost:8000/api/docs"
echo "   - 健康检查: http://localhost:8000/health"

# 等待后端启动
sleep 3

# 启动前端服务
echo "🎨 启动前端服务..."
cd ../frontend
export NVM_DIR="$HOME/.nvm"
[ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh"
nvm use 18
npm run dev &
FRONTEND_PID=$!
echo "✅ 前端服务已启动 (PID: $FRONTEND_PID)"
echo "   - 前端应用: http://localhost:5173"

echo ""
echo "✅ 所有服务已启动！"
echo "   按 Ctrl+C 停止所有服务"

# 等待中断信号
trap "kill $BACKEND_PID $FRONTEND_PID 2>/dev/null; exit" INT TERM
wait

